unit UEmissaoEtiqueta;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Db, DBTables, DBCtrls, Mask, DBGrids,
  ToolEdit, RXDBCtrl, Provider, SqlExpr, DBClient, DBLocal, Jpeg, DBXpress,
  Variants;

type
  TFEmissaoEtiquetaNF = class(TForm)
    Panel2: TPanel;
    BtnConsultar: TBitBtn;
    BtnLimpar: TBitBtn;
    BtnFechar: TBitBtn;
    BtnEtiqueta: TBitBtn;
    GBPeriodo: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    DateEdit1: TDateEdit;
    DateEdit2: TDateEdit;
    edNNotaFiscalINI: TEdit;
    edNNotaFiscalFIM: TEdit;
    Panel1: TPanel;
    QPesquisa: TQuery;
    RadioGroup1: TRadioGroup;
    procedure BtnLimparClick(Sender: TObject);
    procedure BtnFecharClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BtnConsultarClick(Sender: TObject);
    procedure BtnEtiquetaClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    TIPO : STRING;
    { Public declarations }
  end;

var
  FEmissaoEtiquetaNF: TFEmissaoEtiquetaNF;
  str_Filtro : String;

implementation

uses URelatorioEmissaoEtiqueta;

{$R *.dfm}

procedure TFEmissaoEtiquetaNF.BtnLimparClick(Sender: TObject);
begin

  edNNotaFiscalINI.Clear;
  edNNotaFiscalFIM.Clear;

  DateEdit1.Date := 0;
  DateEdit2.Date := 0;

  Panel1.Caption := '';

  edNNotaFiscalINI.SetFocus;

  BtnEtiqueta.Enabled := false;

end;

procedure TFEmissaoEtiquetaNF.BtnFecharClick(Sender: TObject);
begin
  close;
end;

procedure TFEmissaoEtiquetaNF.FormShow(Sender: TObject);
begin
  BtnLimpar.Click;

end;

procedure TFEmissaoEtiquetaNF.BtnConsultarClick(Sender: TObject);
const ct_Sql = ' SELECT DISTINCT (NF_CLIENTE) '+#10+
               ' FROM NOTAFISCALVENDA '+#10+
               '       INNER JOIN TIPOVENDA ON '+#10+
               '             (TIPOVENDA.TIPVEND_CODIGO = NOTAFISCALVENDA.NF_TIPOVENDA) '+#10+
               ' WHERE (NF_SITUACAO = 1) %s '+#10;

      ct_SQL_NF = ' SELECT '+#10+
                  '     NOTAFISCALVENDA.*, '+#10+
                  '     CLIENTE.* '+#10+
                  ' FROM NOTAFISCALVENDA '+#10+
                  '     INNER JOIN CLIENTE ON '+#10+
                  '             (NOTAFISCALVENDA.NF_CLIENTE = CLIENTE.CLI_CODIGO) '+#10+
                  '     INNER JOIN TIPOVENDA ON '+#10+
                  '             (TIPOVENDA.TIPVEND_CODIGO = NOTAFISCALVENDA.NF_TIPOVENDA) '+#10+
                  ' WHERE (NOTAFISCALVENDA.NF_SITUACAO = 1) %s '+#10;
begin

  str_Filtro := '';

    if trim(edNNotaFiscalINI.Text) <> '' then
      str_Filtro := ' AND (NF_NUMERONF >= '+edNNotaFiscalINI.Text+') ';

    if trim(edNNotaFiscalFIM.Text) <> '' then
      str_Filtro := str_Filtro + ' AND (NF_NUMERONF <= '+edNNotaFiscalfim.Text+') ';

    IF DateEdit1.Date <> 0 Then
      str_Filtro := str_Filtro + ' AND (NF_DATAEMISSAO >= '+QuotedStr(FormatDateTime('mm/dd/yyyy 00:00:00',DateEdit1.Date))+') ';

    IF DateEdit2.Date <> 0 Then
      str_Filtro := str_Filtro + ' AND (NF_DATAEMISSAO <= '+QuotedStr(FormatDateTime('mm/dd/yyyy 23:59:59',DateEdit2.Date))+') ';

    QPesquisa.close;
    QPesquisa.SQL.Clear;

    if TIPO = 'ETIQUETA' THEN
    BEGIN

      if str_Filtro = '' then
      begin
        if RadioGroup1.ItemIndex = 1 then
          str_Filtro:='(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''2'')';

        if RadioGroup1.ItemIndex = 0 then
          str_Filtro:='(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''1'')';

      end else begin
        if RadioGroup1.ItemIndex = 1 then
          str_Filtro:=str_Filtro+'AND(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''2'')';

        if RadioGroup1.ItemIndex = 0 then
          str_Filtro:=str_Filtro+'AND(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''1'')';
       end;


      str_Filtro:=str_Filtro+' ORDER BY  NF_NUMERONF ';

      QPesquisa.SQL.Add(Format(ct_Sql,[str_Filtro]));
      QPesquisa.open;

      BtnEtiqueta.Enabled := not(QPesquisa.IsEmpty);

      Panel1.Caption := 'Registro(s) Encontrado(s): '+IntToStr(QPesquisa.RecordCount);

      EXIT;

    END;

  if str_Filtro = '' then
  begin
    if RadioGroup1.ItemIndex = 1 then
       str_Filtro:='(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''2'')';

    if RadioGroup1.ItemIndex = 0 then
       str_Filtro:='(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''1'')';

  end else begin
    if RadioGroup1.ItemIndex = 1 then
       str_Filtro:=str_Filtro+'AND(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''2'')';

    if RadioGroup1.ItemIndex = 0 then
       str_Filtro:=str_Filtro+'AND(TIPOVENDA.TIPVEND_NATUREZAOPERACAO <> ''1'')';

  end;

   str_Filtro:=str_Filtro+' ORDER BY  NF_NUMERONF ';

  QPesquisa.SQL.Add(Format(ct_SQL_NF,[str_Filtro]));
  QPesquisa.open;

  BtnEtiqueta.Enabled := not(QPesquisa.IsEmpty);

  Panel1.Caption := 'Registro(s) Encontrado(s): '+IntToStr(QPesquisa.RecordCount);

  //////////////////////// CODIGO DO PROTOCOLO


end;

procedure TFEmissaoEtiquetaNF.BtnEtiquetaClick(Sender: TObject);
CONST ct_sql = ' SELECT                                      '+#10+
               '        CLI_RAZAO AS RAZAOSOCIAL,            '+#10+
               '        CLI_ENDERECOCORRESP AS ENDERECORESP, '+#10+
               '        CLI_BAIRROCORRESP AS BAIRRORESP,     '+#10+
               '        CLI_CEPCORRESP AS CEPRESP,           '+#10+
               '        CLI_CIDADECORRESP AS CIDADERESP,     '+#10+
               '        CLI_ESTADOCORRESP AS UFRESP,         '+#10+
               '        CLI_ENDERECOPEDIDO AS ENDERECOPEDI,  '+#10+
               '        CLI_BAIRROPEDIDO AS BAIRROPEDI,      '+#10+
               '        CLI_CEPPEDIDO AS CEPPEDI,            '+#10+
               '        CLI_CIDADEPEDIDO AS CIDADEPEDI,      '+#10+
               '        CLI_ESTADOPEDIDO AS UFPEDI,          '+#10+
               '        CLI_IDENTENDCORRESP AS TIPOENDERECO  '+#10+

               ' FROM CLIENTE                                '+#10+

               ' WHERE                                       '+#10+
               '        (CLI_CODIGO IN ( %s ) )              '+#10;
begin

  if TIPO = 'ETIQUETA' THEN
  BEGIN

    str_Filtro := '';
     QPesquisa.First;
    while not(QPesquisa.eof) do
    begin

      str_Filtro := str_Filtro + QPesquisa.Fieldbyname('NF_CLIENTE').AsString + ', ';
      QPesquisa.next;

    end;

    str_Filtro := str_Filtro + QPesquisa.Fieldbyname('NF_CLIENTE').AsString;

    str_Filtro := COPY(str_Filtro,1,LENGTH(str_Filtro) - 1);

    str_Filtro := FORMAT(ct_sql,[str_Filtro]);

    Application.CreateForm(tFRelatorioEmissaoEtiqueta,FRelatorioEmissaoEtiqueta);

    FRelatorioEmissaoEtiqueta.QRelatorio.CLOSE;
    FRelatorioEmissaoEtiqueta.QRelatorio.SQL.CLEAR;
    FRelatorioEmissaoEtiqueta.QRelatorio.SQL.ADD(str_Filtro);
    FRelatorioEmissaoEtiqueta.QRelatorio.OPEN;

    {FRelatorioEmissaoEtiqueta.QuickRep1.Prepare;
    FRelatorioEmissaoEtiqueta.QuickRep1.Preview;}

    FRelatorioEmissaoEtiqueta.QuickRep2.Prepare;
    FRelatorioEmissaoEtiqueta.QuickRep2.Preview;

    EXIT;

  END;
 Application.CreateForm(tFRelatorioEmissaoEtiqueta,FRelatorioEmissaoEtiqueta);
   FRelatorioEmissaoEtiqueta.QuickRep3.Prepare;
    FRelatorioEmissaoEtiqueta.QuickRep3.Preview;

end;

procedure TFEmissaoEtiquetaNF.FormKeyPress(Sender: TObject; var Key: Char);
begin
   if (key = #13) and not ((ActiveControl is TDBMemo) or (ActiveControl is TMemo) or (ActiveControl is TListBox) or (ActiveControl is TDBGrid)) then
    SelectNext(ActiveControl, true, true);
end;

end.
